/* 
 * File:   storage.h
 * Author: alexey
 *
 * Created on 20 Март 2011 г., 1:27
 */

#ifndef STORAGE_H
#define	STORAGE_H

#include <IFile.h>
#include <IDirectory.h>
#include <IStorage.h>
#include <IServiceLocator.h>
#include <string>
#include "../md5/md5.h"

namespace storagemon
{
    class FileObject : public IFile
    {
    public:
        FileObject(const char* path,
                const char* name,
                char md5[MD5_DIGEST_SIZE],
                u_long id,
                u_long parent_id);
        virtual ~FileObject();
    public:
        u_long id() const;
        const char* name() const;
        const char* path() const;
        const char* full_path() const;
        u_long parent_id() const;
        char* md5();
        char* md5_string(char* md5);
    private:
        u_long id_;
        u_long parent_id_;
        char md5_[MD5_DIGEST_SIZE];
        std::string name_;
        std::string path_;
        std::string full_path_;
    };

    IService* CreateStorageServiceFile(const char* name,
            const char* path, u_long id, u_long parent_id,
            char md5[MD5_DIGEST_SIZE]);

    class DirectoryObject : public IDirectory
    {
    public:
        DirectoryObject(const char* name,
                const char* path,
                u_long id,
                u_long parent_id);
        virtual ~DirectoryObject();
    public:
        u_long id() const;
        u_long parent_id() const;
        const char* name() const;
        const char* path() const;
        bool add_file(IFile* file);
        bool remove_file(u_long id);
        u_long size() const;
        std::vector<IFile*>::iterator get_file(u_long id);
    private:
        u_long id_;
        u_long parent_id_;
        std::string name_;
        std::string path_;
        std::vector<IFile*> file_list_;
    };

    IService* CreateStorageServiceDirectory(const char* name,
            const char* path, u_long id, u_long parent_id,
            char md5[MD5_DIGEST_SIZE]);

    class StorageObject : public IStorage
    {
    public:
        StorageObject(const char* name,
                const char* path,
                u_long id);
        virtual ~StorageObject();
    public:
        u_long id() const;
        const char* name() const;
        const char* path() const;
        bool add_dir(IDirectory* dir);
        bool remove_dir(u_long id);
        u_long size() const;
        std::vector<IDirectory*>::iterator get_dir(u_long id);
    private:
        u_long id_;
        std::string name_;
        std::string path_;
        std::vector<IDirectory*> dir_list_;
    };

    IService* CreateStorageServiceStorage(const char* name,
            const char* path, u_long id, u_long parent_id,
            char md5[MD5_DIGEST_SIZE]);

    class StorageServiceFactory : public IStorageServiceFactory
    {
    public:
        StorageServiceFactory();
        virtual ~StorageServiceFactory();
        bool RegisterObject(const char* name, CreateStorageServiceCallback fn);
        bool UnregisterObject(const char* name);
        IService* CreateService(const char* name,
        const char* p_name, const char* p_path, u_long p_id, u_long p_parent_id,
        char p_md5[MD5_DIGEST_SIZE]);
    };
}

#endif	/* STORAGE_H */

